package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.CarGoodsManageEntity;
import com.taxshare.seller.entity.GoodsManageEntity;
import com.taxshare.seller.mapper.CarGoodsManageMapper;
import com.taxshare.seller.view.dto.CarGoodsManageDTO;
import com.taxshare.seller.view.dto.TaxComplilationDTO;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author cxl
 * @date 2021/8/19 11:57
 */
@Service
public class CarGoodsManageService extends ServiceImpl<CarGoodsManageMapper, CarGoodsManageEntity> {

    @Autowired
    CommService commService;

    /**
     * 机动车商品【机动车商品信息新增】
     *
     * @param goodsManageDTO 新增参数
     * @return 结果
     */
    public Response addCarGoods(CarGoodsManageDTO goodsManageDTO) {
        CarGoodsManageEntity goodsManageEntity = new CarGoodsManageEntity();
        BeanUtils.copyProperties(goodsManageDTO, goodsManageEntity);
        goodsManageEntity.setCreateUserid(UserInfoUtil.getUserID());
        goodsManageEntity.setCreateTime(new Date());
        goodsManageEntity.setUuid(UUIDUitl.UUID32(false));
        this.save(goodsManageEntity);
        return Response.success();
    }

    /**
     * 机动车商品【机动车商品信息删除】
     *
     * @param ids 主键ids
     * @return 结果
     */
    public Response removeCarGoods(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.error("SEL_CAR_001");
        }
        this.removeByIds(ids);
        return Response.success();
    }

    /**
     * 机动车商品【机动车商品信息编辑】
     *
     * @param goodsManageDTO 参数
     * @return 结果
     */
    public Response editCarGoods(CarGoodsManageDTO goodsManageDTO) {
        if (goodsManageDTO.getUuid() == null) {
            return Response.error("SEL_CAR_001");
        }
        CarGoodsManageEntity goodsManageEntity = new CarGoodsManageEntity();
        BeanUtils.copyProperties(goodsManageDTO, goodsManageEntity);
        goodsManageEntity.setUpdateUserid(UserInfoUtil.getUserID());
        goodsManageEntity.setUpdateTime(new Date());
        this.updateById(goodsManageEntity);
        return Response.success();
    }

    /**
     * 机动车商品【机动车商品信息查询】
     *
     * @param query
     * @return
     */
    public Response pageCarGoods(PageRequest<TaxComplilationDTO> query) {
        QueryWrapper<CarGoodsManageEntity> queryWrapper = new QueryWrapper();
        TaxComplilationDTO taxComplilationDTO = query.getInfo();

        if (StrUtil.isNotBlank(taxComplilationDTO.getCarCategoryName())) {
            queryWrapper.like("car_category_name", taxComplilationDTO.getCarCategoryName());
        }
        if (StrUtil.isNotBlank(taxComplilationDTO.getTaxRate())) {
            queryWrapper.eq("tax_rate", taxComplilationDTO.getTaxRate());
        }
        if (StrUtil.isNotBlank(taxComplilationDTO.getTaxName())) {
            queryWrapper.like("tax_name", taxComplilationDTO.getTaxName());
        }
        if (StrUtil.isNotBlank(taxComplilationDTO.getTaxCode())) {
            queryWrapper.eq("tax_code", taxComplilationDTO.getTaxCode());
        }
        if (StrUtil.isNotBlank(taxComplilationDTO.getSellerTaxNo())) {
            //销方税号
            queryWrapper.eq("seller_tax_no", taxComplilationDTO.getSellerTaxNo());
        } else {
            List<String> taxNos = commService.getTaxNumbers();
            queryWrapper.in("seller_tax_no", taxNos);//销方税号
        }
        queryWrapper.orderByDesc("create_time");
        IPage<CarGoodsManageEntity> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), CarGoodsManageEntity.class);
        return Response.success(result);
    }

}
